package com.android.model.purchase;

import android.util.Log;

import com.android.bean.ResponseData;
import com.android.bean.postBean.UserPurchase;
import com.android.utils.constraint.Constants;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class GoodsTradingModelimpl implements GoodsTradingModel{
    @Override
    public void goodsTrading(GoodsTradeListener goodsTradeListener,
                             UserPurchase userPurchase) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://47.107.52.7:88/member/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();

        Purchase_interface anInterface
                = retrofit.create(Purchase_interface.class);
        Log.e("getBuyerId",userPurchase.getBuyerId().toString());
        Log.e("getGoodsId",userPurchase.getGoodsId().toString());
        Log.e("getPrice",userPurchase.getPrice().toString());
        Log.e("sellerId",userPurchase.getSellerId().toString());


        Observable<ResponseData<String>> observable
                = anInterface.sendTrading(userPurchase.getBuyerId(),userPurchase.getGoodsId(),
                userPurchase.getPrice(),userPurchase.getSellerId());

        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData<String>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull ResponseData<String> responseData) {
                        goodsTradeListener.onSuccessGoodsTrading(responseData);

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("GoodsTradingModelimpl","请求错误");
                        Log.e("GoodsTradingModelimpl",e.toString());

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }
}
